package com.rybbaby.tss.platform.sysInterfaceDocument.service.imp;

import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.platform.sysFileUseLog.service.ISysFileUseLogService;
import com.rybbaby.tss.platform.sysFiles.service.ISysFilesService;
import com.rybbaby.tss.platform.sysInterfaceDocument.entity.SysInterfaceDocument;
import com.rybbaby.tss.platform.sysInterfaceDocument.service.ISysInterfaceDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import javax.persistence.Table;


/**
 * @description 接口文档数据库操作服务实现
 * 
 * @author 谢进伟
 * 
 * @createDate 2017-01-13 上午11:14:09
 */
@Service
@Transactional
public class SysInterfaceDocumentServiceImp extends BaseServiceImp<SysInterfaceDocument> implements ISysInterfaceDocumentService {

	@Autowired
	private ISysFilesService sysFilesService;
	@Autowired
	private ISysFileUseLogService sysFileUseLogService;

	@Override
	public List<Map<String , Object>> groupIdDatasource(){
		String sql1 = "SELECT id,name FROM sys_document_group ORDER BY createTime desc";
		List<Map<String , Object>> groupId_datasources = this.findForJdbc(sql1 , null);
		return groupId_datasources;
	}

	@Override
	public Serializable save(SysInterfaceDocument entity , String sessionId , String loginUserId , String ... fileRelevances) throws Exception {
		if(entity == null) {
			return null;
		}
		Serializable businessId = save(entity);
		if(businessId != null) {
			if(fileRelevances != null && fileRelevances.length > 0) {
				Table table = SysInterfaceDocument.class.getAnnotation(Table.class);
				String businessTableName = table.name();
				for(String fileRelevance : fileRelevances) {
					if(StringUtils.isNotBlank(fileRelevance)) {
						if(!fileRelevance.contains("=>")) {
							continue;
						}
						String [] fileReleVanceInfo = fileRelevance.split("=>");
						String groupName = fileReleVanceInfo[0];
						String fieldName = fileReleVanceInfo[1];
						if(!sysFilesService.useFile(businessTableName , "id" , businessId , fieldName , groupName , sessionId , loginUserId , null)) {
							throw new Exception("使用文件失败!");
						}
					}
				}
			}
		}
		return businessId;
	}
	
	@Override
	public void updateEntitiy(SysInterfaceDocument entity , String sessionId , String loginUserId , String delteFileIds, String ... fileRelevances) throws Exception {
		if(entity != null && entity.getId() != null) {
			saveOrUpdate(entity);
			Class<? extends SysInterfaceDocument> cls = entity.getClass();
			if(fileRelevances != null && fileRelevances.length > 0) {
				Table table = SysInterfaceDocument.class.getAnnotation(Table.class);
				String businessTableName = table.name();
				for(String fileRelevance : fileRelevances) {
					if(StringUtils.isNotBlank(fileRelevance)) {
						if(!fileRelevance.contains("=>")) {
							continue;
						}
						String [] fileReleVanceInfo = fileRelevance.split("=>");
						String groupName = fileReleVanceInfo[0];
						String fieldName = fileReleVanceInfo[1];
						Field declaredField = cls.getDeclaredField(fieldName);
						declaredField.setAccessible(true);
						Object oldValue = declaredField.get(entity);
						if(!sysFilesService.updateUsedFile(businessTableName , "id" , entity.getId() , fieldName , groupName , sessionId , loginUserId , delteFileIds , oldValue+"")) {
							throw new Exception("使用文件失败!");
						}
					}
				}
			}
		}
	}
	
}
